/**
 * 
 */
package com.istart.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;
import org.apache.curator.utils.CloseableUtils;

/**
 * @author 谢进军
 *
 */
public class CreateClientExample {
	
	private static final String PATH = "/example/basic";
	
	public static void main(String[] args) throws Exception {
		TestingServer server = new TestingServer();
		CuratorFramework client = null;
		System.out.println(server.getConnectString());
		client = createSimple(server.getConnectString());
		client.start();
		client.create().creatingParentContainersIfNeeded().forPath(PATH, "test".getBytes());
		CloseableUtils.closeQuietly(client);
		 client = createWithOptions(server.getConnectString(), new ExponentialBackoffRetry(1000, 3), 1000, 1000);
         client.start();
         System.out.println(new String(client.getData().forPath(PATH)));
	}
	
	public static CuratorFramework createSimple(String connectString){
		ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
		return CuratorFrameworkFactory.newClient(connectString, retryPolicy);
	}
	
	public static CuratorFramework createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs){
		 return CuratorFrameworkFactory.builder().connectString(connectionString)
	                .retryPolicy(retryPolicy)
	                .connectionTimeoutMs(connectionTimeoutMs)
	                .sessionTimeoutMs(sessionTimeoutMs)
	                // etc. etc.
	                .build();
	}
}
